#!usr/bin/env python3
# -*- coding utf-8 -*-
from sqlalchemy  import Column,String,create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

print("sqlalchemy框架(ORM)")

Base = declarative_base()

class StuClass(Base):

	__tablename__='stu_class' #设置对应的表
	
	stu_id = Column(String(20),primary_key=True)
	stu_name = Column(String(20))
	stu_score = Column(String(20))
	
#创建数据库引擎
#  mysql+mysqlconnector://root:password@localhost:3306/test 
#参数格式 '数据库类型+数据库驱动名称://用户名:口令@机器地址:端口号/数据库名'
engine = create_engine("mysql+mysqlconnector://root:@localhost:3306/stu");

#绑定session 
DbSession = sessionmaker(bind=engine) #是一个类

session = DbSession();

#增加
def add(stucls):
	session.add(nclass) #添加到session
	session.commit() #这步才真正提交到数据库
	
def delete(stucls):
	print(session.delete)
	session.delete()
	session.commit()
	
nclass = StuClass(stu_id='null',stu_name='kogfu',stu_score=65)
delcls = StuClass(stu_id='12')

delete(delcls)

session.close()


